package in.falkeninc.umt_v0_9_8_1;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import com.actionbarsherlock.app.SherlockFragment;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;

import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.ToggleButton;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabHost.TabSpec;

/**
 * This class is the main UI fragment. This is where the user adds or updates alarms.
 * @author Eivind Falkenstein
 * @category Details View
 *
 */
/**
 * @author equex
 *
 */
@SuppressLint("SimpleDateFormat")
public class DetailsFragment extends SherlockFragment implements View.OnClickListener, AdapterView.OnItemSelectedListener, OnCheckedChangeListener  {
	
	/**
	 * Must include alarm ID here as its the only link between the list and the new fragment, which does not access TitlesFragment
	 * Index is the selected index of the ListView. (onListItemClick in TitlesFragment)
	 * Supply index input as an argument.
	 * 
	 * Both are -1 if invalid.
	 * 
	 * Do not include more stuff here, use DB to lookup the data instead for passing data between fragments.
	 * 
	 * @param index
	 * @param alarmid
	 * @return
	 */

	private String code_days_of_week = "x";				// Like the bus tables in Norway: Daily excluding day 6 and 7 (Saturday and Sunday). Day 1 is Monday.
	private String newdaycode_enabled = "";
	private String newdaycode_disabled = "1234567";		// UI togglebuttons are initially untoggled

	private int repeats_selected;
	private int icon_selected;

	private TabHost tabHost;

	private static DetailsFragment f;
	private AlarmScheduler as;
	private Context ctx;
	
	/**
	 * This is the only link between MainActivity and this fragment
	 * @param index
	 * @param alarmid
	 * @return
	 */
	public static DetailsFragment newInstance(int index, int alarmid) {

		f = new DetailsFragment();

		Bundle args = new Bundle();
		args.putInt("index", index);
		args.putInt("alarmid", alarmid);
		f.setArguments(args);

		return f;	
	}

	/* (non-Javadoc)
	 * @see android.support.v4.app.Fragment#onCreateView(android.view.LayoutInflater, android.view.ViewGroup, android.os.Bundle)
	 */
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

		ctx	=	getSherlockActivity();

		if (container == null) {
		 // We have different layouts, and in one of them this fragment's containing frame doesn't exist. The fragment
		 // may still be created from its saved state, but there is no reason to try to create its view hierarchy because it
		 // won't be displayed. Note this is not needed -- we could just run the code below, where we would create and return
		 // the view hierarchy; it would just never be used.
			
			return null;
		}

		// If non-null, this is the parent view that the fragment's UI should be attached to. The fragment should not add the view
		// itself, but this can be used to generate the LayoutParams of the view.
	
		return inflater.inflate(R.layout.newalarm, container, false);
			
	}
	
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {

		super.onActivityCreated(savedInstanceState);

		int aid = getAlarmID();

		setup_tabs();
		
		Button bt1 = (Button)getSherlockActivity()	// We need to have listeners on the buttons. We decide later how to deal with the appearance of the button a bit down.
				.findViewById(R.id.btnDeleteAlarm);
		
		bt1.setOnClickListener(this);				// Attach click listeners here instead of in XML because XML only hits its own context (main or details)		
		bt1.setEnabled(true);						// because we might disable it later, reset before hitting logic again.

		Button bt2 = (Button)getSherlockActivity()
				.findViewById(R.id.btnOK);
		
		bt2.setOnClickListener(this);
		bt2.setText("Add");
		
		add_togglebutton_listeners_weekdays();
		
        if (aid != -1) {			
			bt2.setText("Update");					// We have some sort of ID so set button text to 'Update' instead
			populate_UI_with_DB(aid);				// Populate interface with values from the database.
		} else {
			bt1.setEnabled(false);					// Cannot delete an alarm under construction.
			populate_UI_with_defaults();			// We have no ID so this must be a new alarm, so populate with clever default values
		}
	}
	
	/**
	 * Sets all the UI widgets to sensible values for a new alarm
	 */
	public void populate_UI_with_defaults() {

		SherlockFragmentActivity sfa = (SherlockFragmentActivity) getActivity();
		TextView tv;
		CheckBox cb;
		TimePicker tp;
		
		Calendar calendar = Calendar.getInstance();


		/* -------------------------------------------------------------------------------------------------------------------*/

		// *** ALARM ID ***
		tv = (TextView)sfa.findViewById(R.id.txtAlarmID);
		
		// Fragment doesnt exist at this time
		if (tv == null) {
			return;
		}
		
		/* -------------------------------------------------------------------------------------------------------------------*/

		// Initialize the icon spinner.
		Spinner spinnericon = (Spinner)getSherlockActivity().findViewById(R.id.spnIcon);

        int[] array = getResources().getIntArray(R.array.minusIcons);
        String [] objects = new String[array.length];
        for(int i = 0; i != array.length; i++)
        {
            objects[i] = "" + array[i];
        }
        spinnericon.setOnItemSelectedListener(this);
        spinnericon.setAdapter(new SpinnerCustomAdapter(getSherlockActivity(), R.id.spnIcon, objects)); 

        Spinner spinnerreps = (Spinner)sfa.findViewById(R.id.spnRepeats);
		// Create an ArrayAdapter using the string array and a default spinner layout
		ArrayAdapter<CharSequence> adapter_spinner5x = ArrayAdapter.createFromResource(getSherlockActivity(), R.array.repeats, android.R.layout.simple_spinner_item);
		// Specify the layout to use when the list of choices appears
		adapter_spinner5x.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		// Apply the adapter to the spinner
		spinnerreps.setAdapter(adapter_spinner5x);
		spinnerreps.setOnItemSelectedListener(this);
		
		// get the Alarm ID value, this is the only data that gets passed from the DB to GUI and back trough this controller method 		
		tv.setText("-1");

		/* -------------------------------------------------------------------------------------------------------------------*/

		/** ALARM NAME & DESCRIPTION
		 * Has their defaults in android:hint string
		 */

		/** TIMEPICKER WHEN
		 * Auto-set to current by TimePicker object
		 * Set hour format 12/24
		 */
		tp = (TimePicker)sfa.findViewById(R.id.pickAlarmTime);
		tp.setIs24HourView(true);

		int h = calendar.get(Calendar.HOUR_OF_DAY);			// It's not correctly setting the currentHour to the new 24 hour format (at least on jelly, people say)
	    int m = calendar.get(Calendar.MINUTE);
	    tp.setCurrentHour(h);
	    tp.setCurrentMinute(m);

	    /** THRESHOLD WHEN
		 * Auto-set to current by TimePicker object
		 */
		tp = (TimePicker)sfa.findViewById(R.id.pickThresholdTime);
		tp.setIs24HourView(true);

		h = calendar.get(Calendar.HOUR_OF_DAY);
	    m = calendar.get(Calendar.MINUTE);
	    tp.setCurrentHour(h);
	    tp.setCurrentMinute(m);

		/** ENABLE/DISABLE
		 * Auto-set to Checked
		 */
		cb = (CheckBox)sfa.findViewById(R.id.chkAlarmEnabled);
		cb.setChecked(true);
				
	}
	
	/**
	 * Sets all the UI widgets to values from the DB, gotten by R.id.myid
	 * In effect we are editing an existing alarm, so this should end in an
	 * SQL update 
	 */
	public void populate_UI_with_DB(int aid) {

		// Use temporaries for them all
		SherlockFragmentActivity sfa =  getSherlockActivity();
		TextView tv;
		CheckBox cb;
		boolean cbs;
		TimePicker tp;
		Calendar calendar = Calendar.getInstance();
		
		// Get database handler to fill in the UI components with actual data from the DB
		DatabaseHandler db = new DatabaseHandler(getSherlockActivity());
		
		// Fetch currently UI selected/returned ID
		AlarmInfo ai = db.getAlarm(aid);

		// TODO: Populate with DB lookups here		

		/* -------------------------------------------------------------------------------------------------------------------*/
		// *** ICON SPINNER ***
		Spinner spinnericon = (Spinner)getSherlockActivity().findViewById(R.id.spnIcon);
        int[] array = getResources().getIntArray(R.array.minusIcons);
        String [] objects = new String[array.length];
        for(int i = 0; i != array.length; i++){
            objects[i] = "" + array[i];
        }

        // Adapter FIRST!
        spinnericon.setAdapter(new SpinnerCustomAdapter(getSherlockActivity(), R.id.spnIcon, objects)); 
        spinnericon.setSelection(ai.getIcon());
        spinnericon.setOnItemSelectedListener(this);

		/* -------------------------------------------------------------------------------------------------------------------*/

        // *** ALARM ID ***
		tv = (TextView)sfa.findViewById(R.id.txtAlarmID);
		// get the Alarm ID value, this is the only data that gets passed from the DB to GUI and back trough this controller method 		
		tv.setText("AlarmID: (" + String.valueOf(getAlarmID()) + ") ");

		// *** ALARM NAME ***
		tv = (TextView)sfa.findViewById(R.id.id_enter_medicine_name);
		tv.setText(ai.getName());
				
		// *** ALARM DESCRIPTION ***
		tv = (TextView)sfa.findViewById(R.id.id_description);
		tv.setText(ai.getDescription());
		db.close();
		
		tp = (TimePicker)sfa.findViewById(R.id.pickAlarmTime);
		tp.setIs24HourView(true);

		int h = calendar.get(Calendar.HOUR_OF_DAY);			// It's not correctly setting the currentHour to the new 24 hour format (at least on jelly, people say)
	    int m = calendar.get(Calendar.MINUTE);
	    tp.setCurrentHour(h);
	    tp.setCurrentMinute(m);

	    /** THRESHOLD WHEN
		 * Auto-set to current by TimePicker object
		 */
		tp = (TimePicker)sfa.findViewById(R.id.pickThresholdTime);
		tp.setIs24HourView(true);

		h = calendar.get(Calendar.HOUR_OF_DAY);
	    m = calendar.get(Calendar.MINUTE);
	    tp.setCurrentHour(h);
	    tp.setCurrentMinute(m);
		
		// *** ENABLE/DISABLE CHECKBOX ***
		cb = (CheckBox)sfa.findViewById(R.id.chkAlarmEnabled);
		// convert integer to boolean (SQLite doesn't take booleans)
		cbs = (ai.getEnabled() == 0 ? false : true);
		cb.setChecked(cbs);
		
		// *** REPEATS SPINNER ***
		// Same as for default, except we set the selected spinner item to the value from the DB (no need to store 1,2,3,4,5 for all alarms.)
		Spinner spinner = (Spinner)getSherlockActivity().findViewById(R.id.spnRepeats);
		// Create an ArrayAdapter using the string array and a default spinner layout
		ArrayAdapter<CharSequence> adapter_spinner5x = ArrayAdapter.createFromResource(getSherlockActivity(),R.array.repeats, android.R.layout.simple_spinner_item);
		// Specify the layout to use when the list of choices appears
		adapter_spinner5x.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);		
		// Apply the adapter to the spinner before modifying it!
		spinner.setAdapter(adapter_spinner5x);
		spinner.setSelection(ai.getRepeats());
        spinner.setOnItemSelectedListener(this);

/* -------------------------------------------------------------------------------------------------------------------*/
		
		// clean up and leave the party
		db.close();			
	}


	public int getShownIndex() {
		try {
			return getArguments().getInt("index", 0);
		} catch (Exception e) {
			e.printStackTrace();
			// return -1 if no UI selection done yet.
			return -1;
		}
	}	

	@SuppressWarnings("finally")
	public int getAlarmID() {

		TextView tv = (TextView)getSherlockActivity().findViewById(R.id.txtAlarmID);

		int res = -1;

		if (tv != null) {
			try {
                res = getArguments().getInt("alarmid");
			} catch (Exception e) {								
				return res;
			} finally {
				return res;
			}
		}
		return res;
	}

	@Override
	public void onClick(View v) {

		/* If both fragments are visible (TitleFragment and DetailsFragment) then don't finish activity (that would exit the main activity!)
		 Instead, update TitlesFragment's view */
		View detailsFrame = getSherlockActivity().findViewById(R.id.details);
		boolean mDualPane = detailsFrame != null && detailsFrame.getVisibility() == View.VISIBLE;

		switch (v.getId()) {

			case(R.id.btnDeleteAlarm):
			
				deleteAlarmTotally(v);

				// Not dual pane, so details fragment is inside its own DetailsActivity (that we can finish)
				if (!mDualPane) {
					
					getSherlockActivity().finish();
					
				} else {
					
					// Forces update on TitlesFragment so alarms appear as they are added to the list in dual pane mode.
					// For this to work on 2.3, dbShowAll had to be called from onPause
					TitlesFragment titles = (TitlesFragment)getSherlockActivity().getSupportFragmentManager().findFragmentById(R.id.titles);
					if (titles != null) titles.update();
					
				}
				
				break;

				/* -------------------------------------------------------------------------------------------------------------------*/
	
			case(R.id.btnOK):
				
				addNewAlarmTotally(v);
				trigger_alarm_scheduler();

				// Same logic as above
				if (!mDualPane) {
					
					getSherlockActivity().finish();
					
				} else {
	
					TitlesFragment titles = (TitlesFragment)getSherlockActivity().getSupportFragmentManager().findFragmentById(R.id.titles);					
					if (titles != null) titles.update();
					
				}
				
				break;

				/* -------------------------------------------------------------------------------------------------------------------*/

			case (R.id.tglbtn_mon):
				break;
			case (R.id.tglbtn_tue):
				break;
			case (R.id.tglbtn_wed):
				break;
			case (R.id.tglbtn_thu):
				break;
			case (R.id.tglbtn_fri):
				break;
			case (R.id.tglbtn_sat):
				break;
			case (R.id.tglbtn_sun):
				break;
			
			default:
				break;
		}
	}
	
	/**
	 * This function just makes the onCreate() look tidier
	 */
	private void setup_tabs()
	{
		tabHost=(TabHost)getSherlockActivity().findViewById(R.id.tabhost);

/*		Helo i am not here please go away 
		Dual pane switch crashes back to portrait, because fragment is gone, so return if something is gone, 
		ex. the tabhost. */
		if (tabHost == null)
			return;			

		tabHost.setup();
		
		TabSpec spec1=tabHost.newTabSpec("Basic");
		spec1.setContent(R.id.tab1);
		spec1.setIndicator("Basic");

		TabSpec spec2=tabHost.newTabSpec("Advanced");
		spec2.setContent(R.id.tab2);
		spec2.setIndicator("Advanced");

		TabSpec spec3=tabHost.newTabSpec("Adv. II");
		spec3.setContent(R.id.tab3);
		spec3.setIndicator("Adv. II");

		tabHost.addTab(spec1);
		tabHost.addTab(spec2);
		tabHost.addTab(spec3);

		tabHost.setCurrentTab(0);      

		tabHost.setOnTabChangedListener(new OnTabChangeListener() {
			
			@Override
			public void onTabChanged(String tabId) {
				if (tabId.equals("1")) {
					//getSherlockActivity().findViewById(R.id.scroller1).requestFocus();
				}
				if (tabId.equals("2")) {
					//getSherlockActivity().findViewById(R.id.scroller2).requestFocus();
				}
					if (tabId.equals("3")) {
					//getSherlockActivity().findViewById(R.id.scroller3).requestFocus();
					}
			}
		});
	}
	
	/**
	 * This function just makes the constructor look tidier
	 */
	private void add_togglebutton_listeners_weekdays() {
		ToggleButton tgl;
		tgl = (ToggleButton) getSherlockActivity().findViewById(R.id.tglbtn_mon);
		tgl.setOnCheckedChangeListener(this);
		tgl = (ToggleButton) getSherlockActivity().findViewById(R.id.tglbtn_tue);
		tgl.setOnCheckedChangeListener(this);
		tgl = (ToggleButton) getSherlockActivity().findViewById(R.id.tglbtn_wed);
		tgl.setOnCheckedChangeListener(this);
		tgl = (ToggleButton) getSherlockActivity().findViewById(R.id.tglbtn_thu);
		tgl.setOnCheckedChangeListener(this);
		tgl = (ToggleButton) getSherlockActivity().findViewById(R.id.tglbtn_fri);
		tgl.setOnCheckedChangeListener(this);
		tgl = (ToggleButton) getSherlockActivity().findViewById(R.id.tglbtn_sat);
		tgl.setOnCheckedChangeListener(this);
		tgl = (ToggleButton) getSherlockActivity().findViewById(R.id.tglbtn_sun);
		tgl.setOnCheckedChangeListener(this);
	}
	
	
	public void trigger_alarm_scheduler()
	{
		as = new AlarmScheduler();	
     	as.reschedule_alarms(this.ctx);

	}


	public void deleteAlarmTotally(View v)  {
		
		DatabaseHandler db = new DatabaseHandler(getSherlockActivity());
		AlarmInfo ai = new AlarmInfo();
		SherlockFragmentActivity sfa = getSherlockActivity();
		
		TextView nameet = (TextView)sfa.findViewById(R.id.txtAlarmID);
		
		try {
			ai.setID(getAlarmID());
			Log.d("deletealarm: ", (String) nameet.getText());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		db.deleteAlarm(ai);
		
		Log.d("DetailsFragment.deleteAlarmTotally ", "called");
	}
	
	/**
	 * @param v The view
	 * @return An integer with the ID of the resulting INSERT 
	 */
	@SuppressWarnings("unused")
	public long addNewAlarmTotally(View v)  {

		/* Couple a objects. Should of dun this before */
		SherlockFragmentActivity sfa = getSherlockActivity();
		DatabaseHandler db = new DatabaseHandler(getSherlockActivity());

		SimpleDateFormat formatter = new SimpleDateFormat("yyyy.MM.dd");
		SimpleDateFormat modformatter = new SimpleDateFormat("yyyy.MM.dd HH:mm");
		TimePicker tp = null;
		Date curDate = new Date(), oldDate;
		AFW_Time t = new AFW_Time();

		CheckBox tmp = null;
		Spinner spn = null;		
		EditText nameet, descet  = null;
		String desc = null, name = null,  curTime = null, modTime = null;
		AlarmInfo ai = null; 
		TextView tv = null;
		
		int hour = 0, min = 0, hour_thr = 0, min_the = 0, enabled = 0, snooze = 0, alarmid =  -1;
		long curMillis = 0, oldMillis = 0, alarmtime = 0; 
		
		tv = (TextView)sfa.findViewById(R.id.txtAlarmID);

		// TextView doesn't exist so fragment is in limbo, return for now
		if (tv == null) return -1; 

		alarmid = getAlarmID();
		if (alarmid == -1) {
			// ID is missing (so new alarm) so go ahead and add as normal
		} else {
			// id was set, so delete the current alarm (we know it exists now so no null check)
			// TODO: Not very nice
			deleteAlarmTotally(tv);
			// now go ahead and add alarm again, in effect an update.
		}

		/* Get the current hour and minute from the TimePicker on screen */
		tp = (TimePicker)sfa.findViewById(R.id.pickAlarmTime);
	
		hour = tp.getCurrentHour();
		min = tp.getCurrentMinute();
	   	
		/* Get the medicine name from UI */
		nameet= (EditText)sfa.findViewById(R.id.id_enter_medicine_name);
		name = nameet.getText().toString();
	
	   	descet = (EditText)sfa.findViewById(R.id.id_description);
	   	desc = descet.getText().toString();

	   	formatter.setLenient(false);

	   	curMillis = curDate.getTime();
	   	curTime = formatter.format(curDate);  
	   	
	   	modTime = curTime + " " + String.valueOf(hour) + ":" + String.valueOf(min);
	
	   	try {
			oldDate = modformatter.parse(modTime);
			oldMillis = oldDate.getTime(); 
		} catch (ParseException e) {
			e.printStackTrace();
		}
   	
	   	alarmtime = oldMillis;
	   	
		tmp = (CheckBox)sfa.findViewById(R.id.chkAlarmEnabled);
		enabled = tmp.isChecked() ? 1 : 0;

		spn = (Spinner)sfa.findViewById(R.id.spnIcon);
		int icon = this.icon_selected;
		
		spn = (Spinner)sfa.findViewById(R.id.spnRepeats);
		int rep = this.repeats_selected;
		
		tmp = (CheckBox)sfa.findViewById(R.id.chkSnooze);
		snooze = tmp.isChecked() ? 1 : 0;
		
		ai = new AlarmInfo(name, desc, alarmtime, icon, enabled, rep, code_days_of_week, snooze); 

		return db.addAlarm(ai);			// return the long auto-increment from SQLite insert

   }

	/* broadcast a custom intent. */ 
	public void broadcastIntent(View view) {
      Intent intent = new Intent();
      intent.setAction("in.falkeninc.umt_v0_9_8_1.CUSTOM_INTENT_ADDALARM");
      getSherlockActivity().sendBroadcast(intent);
      
   }


	@Override
	public void onItemSelected(AdapterView<?> av, View v, int position, long arg3) {
		switch (av.getId()) {
		case (R.id.spnIcon):
			Log.d("Spinner spnIcon onclick: ", String.valueOf(position));
			this.icon_selected = position;
			break;		
		case (R.id.spnRepeats):
			Log.d("Spinner spnRepeats onclick: ", String.valueOf(position));
			this.repeats_selected = position;
			break;
		default:
			Log.d("Spinner MISSED onclick: ", String.valueOf(position));
			break;
		}				
	}

	@Override
	public void onNothingSelected(AdapterView<?> arg0)
	{
		// TODO Auto-generated method stub
	}

	@Override
	public void onCheckedChanged(CompoundButton btn, boolean isChecked) {
		
		switch (btn.getId()) {										// for our Day of Week ToggleButtons, we will make a string like this: dx67
		
		case (R.id.tglbtn_mon):
			day_of_week_helper("1", isChecked);
			break;
		case (R.id.tglbtn_tue):
			day_of_week_helper("2", isChecked);
			break;
		case (R.id.tglbtn_wed):
			day_of_week_helper("3", isChecked);
			break;
		case (R.id.tglbtn_thu):
			day_of_week_helper("4", isChecked);
			break;
		case (R.id.tglbtn_fri):
			day_of_week_helper("5", isChecked);
			break;
		case (R.id.tglbtn_sat):
			day_of_week_helper("6", isChecked);
			break;
		case (R.id.tglbtn_sun):
			day_of_week_helper("7", isChecked);
			break;
		default:
			break;
		}		
		this.code_days_of_week = generate_dayofweekcode(newdaycode_enabled, newdaycode_disabled);		
	}

	private void day_of_week_helper(String day, boolean isChecked) {	
		if (isChecked) {
			newdaycode_enabled += day;
			newdaycode_disabled = newdaycode_disabled.replace(day, "");
		} else {
			newdaycode_disabled += day;
			newdaycode_enabled = newdaycode_enabled.replace(day, "");
		}
	}

	/**
	 * From two strings of day-numberstrings enabled and disabled , generate a resulting string that is readable 
	 * @param newdaycode_enabled
	 * @param newdaycode_disabled
	 * @return
	 */
	private String generate_dayofweekcode(String newdaycode_enabled, String newdaycode_disabled) {
		
		AFW_String s = new AFW_String();
		String res = "n/a";

		// Sort first!
		newdaycode_enabled 	= s.sort(newdaycode_enabled);
		newdaycode_disabled = s.sort(newdaycode_disabled);
		
		// The most usual cases: Daily and Daily except Saturday and Sunday
		if (newdaycode_enabled.equals("1234567")) {
			return "d";														// Every day of the week is simply a 'd'
		} else if (newdaycode_disabled.equals("1234567")) {
			return "x";														// Every day has been disabled, so just x
		}
				
		if (newdaycode_enabled.length() >= newdaycode_disabled.length()) {
			res = "dx" + newdaycode_disabled;								// Daily except a few excluded
		} else {
			res = "d" + newdaycode_enabled + "x" + newdaycode_disabled;		// Else return a mix
		}
		
		return res;
	}
	
}

